package com.main.hotel.service.impl;

import com.main.hotel.entity.Room;
import com.main.hotel.entity.RoomType;
import com.main.hotel.repository.RoomRepository;
import com.main.hotel.repository.RoomTypeRepository;
import com.main.hotel.service.RoomService;
import com.main.hotel.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
//@Transactional
public class RoomTypeServiceImpl implements RoomTypeService {
@Autowired
    private RoomTypeRepository roomTypeRepository;

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomRepository roomRepository;

    @Override

    public int addroomtype(RoomType roomType) {
        roomTypeRepository.save(roomType);
        return 1;
    }


    @Override
    public int deleteRoomTypeById(Integer id) {
        roomTypeRepository.deleteById(id);
        return 1;
    }

    @Override
    public int updataRoomType(RoomType roomType) {
        RoomType roomType1=new RoomType();
        roomType1.setRoomtype_id(roomType.getRoomtype_id());
        roomType1.setBedNum(roomType.getBedNum());
        roomType1.setLiveNum(roomType.getLiveNum());
        roomType1.setPrice(roomType.getPrice());
        roomType1.setName(roomType.getName());
        roomType1.setRoomNum(roomType.getRoomNum());
        roomTypeRepository.save(roomType1);
        return 1;
    }

    @Override
    public Page<RoomType> findAll(int page, int pageSize) {
        Pageable pageable= PageRequest.of(page,pageSize);
        return roomTypeRepository.findAll(pageable);
    }


    @Override
    public RoomType findByCustomerId(int roomtype_id) {
        RoomType roomType=roomTypeRepository.findByCustomerId(roomtype_id);
        return roomType;
    }
  @Override
  public float getRoomPrice(String name) {
    float price=roomTypeRepository.getRoomPrice(name);
    System.out.println(price);
    System.out.println("00");
    return price;
  }
    @Override
    public Boolean initRoomNum() {
        for (RoomType r : roomTypeRepository.findAll()
        ) {
            r.setRoomNum(roomService.countRoomNum(r.getRoomtype_id()));
            roomTypeRepository.save(r);
        }
        return true;
    }

    @Override
    public Boolean initNameByRoomType_id() {
        for (Room room : roomRepository.findAll()
        ) {
            room.setRoom_type(roomTypeRepository.findNameByRoomtype_id(room.getRoomtype_id()));
            roomRepository.save(room);
        }
        return true;
    }
}
